package com.gsn.chooseChannel;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.gsn.dataCommon.ConfigModel;
import com.gsn.dataCommon.UserData;
import com.gsn.engine.template.GsnEnableButton;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class ListChannel extends Group implements ClickListener {

	public int numChannel = 6;
	public GsnEnableButton channel[];

	public ListChannel() {
		PortalTexture portalTexture = PortalTexture.inst();
		channel = new GsnEnableButton[6];
		channel[0] = new GsnEnableButton(0, "0", portalTexture.iconChannel0, portalTexture.iconChannel0Press, portalTexture.iconChannel0Disable);
		portalTexture.scaleActor(channel[0]);

		channel[1] = new GsnEnableButton(1, "1", portalTexture.iconChannel1, portalTexture.iconChannel1Press, portalTexture.iconChannel1Disable);
		portalTexture.scaleActor(channel[1]);

		channel[1].y = channel[0].getRealHeight();

		channel[2] = new GsnEnableButton(2, "2", portalTexture.iconChannel2, portalTexture.iconChannel2Press, portalTexture.iconChannel2Disable);
		portalTexture.scaleActor(channel[2]);

		channel[2].y = channel[1].y + channel[1].getRealHeight();

		channel[3] = new GsnEnableButton(3, "3", portalTexture.iconChannel3, portalTexture.iconChannel3Press, portalTexture.iconChannel3Disable);
		portalTexture.scaleActor(channel[3]);

		channel[3].y = channel[2].y + channel[2].getRealHeight();

		channel[4] = new GsnEnableButton(4, "4", portalTexture.iconChannel4, portalTexture.iconChannel4Press, portalTexture.iconChannel4Disable);
		portalTexture.scaleActor(channel[4]);

		channel[4].y = channel[3].y + channel[3].getRealHeight();

		channel[5] = new GsnEnableButton(5, "5", portalTexture.iconChannel5, portalTexture.iconChannel5Press, portalTexture.iconChannel5Disable);
		portalTexture.scaleActor(channel[5]);
		int i;
		for (i = 5; i >= 1; i--) {
			channel[i - 1].y = channel[i].y + channel[i].getRealHeight();
		}
		for (i = 5; i >= 0; i--) {
			addActor(channel[i]);
			// channel[i].setAndSaveClickListener(this);
			final int _i = i;
			channel[i].setAndSaveClickListener(new ClickListener() {

				@Override
				public void click(Actor actor, float x, float y) {
					// TODO Auto-generated method stub
					PokerSound.playClick();
					if (!MyPoker.game.statePortal.equals(StatePortal.CHOOSE_CHANNEL))
						return;
					int betLevel = betLevel();
					if (betLevel >= _i) {
						ConfigModel.selectedChannel = _i;
						MyPoker.client.send(PacketFactory.createSelectChannel(_i));
						MyPoker.game.statePortal = StatePortal.CHOOSING_CHANNEL;
					}

				}
			});
		}

		width = channel[0].getRealWidth();
		height = numChannel * channel[0].getRealHeight();

	}

	public void updateChannel() {

		int betLevel = betLevel();

		int i;
		for (i = 0; i < 6; i++) {

			switch (i) {
			case 0:
				if (betLevel == 0 || betLevel == 1)

					channel[i].setEnable(true);

				else {
					channel[i].setEnable(false);
				}
				break;

			case 1:
				// if (betLevel == 1 || betLevel == 2)
				if (betLevel >= 1)
					channel[i].setEnable(true);
				else {
					channel[i].setEnable(false);

				}
				break;
			case 2:
				// if (betLevel == 2 || betLevel == 3)
				if (betLevel >= 2)
					channel[i].setEnable(true);
				else {
					channel[i].setEnable(false);

				}
				break;
			case 3:
				// if (betLevel == 3 || betLevel == 4)
				if (betLevel >= 3)
					channel[i].setEnable(true);
				else {
					channel[i].setEnable(false);

				}
				break;
			case 4:
				// if (betLevel == 4 || betLevel == 5)
				if (betLevel >= 4)
					channel[i].setEnable(true);
				else {
					channel[i].setEnable(false);

				}
				break;
			case 5:
				// if (betLevel == 5)
				if (betLevel >= 5)
					channel[i].setEnable(true);
				else {
					channel[i].setEnable(false);

				}
				break;

			default:
				break;
			}

		}
	}

	public int betLevel() {
		int i;
		if (UserData.gold == 0)
			return -1;
		for (i = 0; i < ConfigModel.channelModelList.length; i++) {
			if (ConfigModel.channelModelList[i].goldEnd >= UserData.gold) {
				if (ConfigModel.channelModelList[ConfigModel.selectedChannel].bets[0] * 3 <= UserData.gold)
					return i;
				else
					return i - 1;
			}
		}

		return i;
	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub

	}

}
